package com.bjb.vr.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.ao.OptFeatureAo;
import com.bjb.vr.common.constant.AliOssConstants;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.OptFeatureConfig;
import com.bjb.vr.common.dto.ResContent;
import com.bjb.vr.common.dto.ResDictionary;
import com.bjb.vr.common.mapper.OptFeatureConfigMapper;
import com.bjb.vr.common.mapper.ResContentMapper;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.utils.AliyunOssUtil;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.OptFeatureVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author admin
 * @description 针对表【opt_feature_config】的数据库操作Service实现
 * @createDate 2022-12-13 17:13:27
 */
@Service
public class OptFeatureConfigService extends ServiceImpl<OptFeatureConfigMapper, OptFeatureConfig> {

    private static AliyunOssUtil ossUtil = null;

    static {
        ossUtil = new AliyunOssUtil.Builder().build(AliOssConstants.ENDPOINT, AliOssConstants.ACCESS_KEY_ID, AliOssConstants.ACCESS_KEY_SECRET);
    }

    @Resource
    private OptFeatureConfigMapper featureConfigMapper;

    @Resource
    private ResContentMapper contentMapper;

    @Resource
    private ResDictionaryService dictionaryService;


    public List<OptFeatureVo> queryFeatureList(OptFeatureAo optFeatureAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        LambdaQueryWrapper<OptFeatureConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(OptFeatureConfig::getOrderNum);
        List<OptFeatureConfig> optFeatureConfigs = featureConfigMapper.selectList(queryWrapper);
        List<OptFeatureVo> featureVoList = new ArrayList<>();
        for (OptFeatureConfig featureConfig : optFeatureConfigs) {
            OptFeatureVo featureVo = new OptFeatureVo();
            BeanUtils.copyProperties(featureConfig, featureVo);
            featureVo.setResImage(ossUtil.getFileUrl(featureVo.getResImage()));
            featureVoList.add(featureVo);
        }
        return featureVoList;
    }

    public OptFeatureVo queryFeatureOne(OptFeatureAo optFeatureAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        OptFeatureConfig featureConfig = featureConfigMapper.selectById(optFeatureAo.getId());
        OptFeatureVo featureVo = new OptFeatureVo();
        BeanUtils.copyProperties(featureConfig, featureVo);
        featureVo.setResImage(ossUtil.getFileUrl(featureVo.getResImage()));
        if (StringUtils.isNotBlank(featureVo.getPeriod())) {
            featureVo.setPeriodCode(dictionaryService.getDictionaryByName(featureVo.getPeriod()).getDictCode());
        }
        if (StringUtils.isNotBlank(featureVo.getLevel())) {
            featureVo.setLevelCode(dictionaryService.getDictionaryByName(featureVo.getLevel()).getDictCode());
        }
        if (StringUtils.isNotBlank(featureVo.getSubject())) {
            featureVo.setSubjectCode(dictionaryService.getDictionaryByName(featureVo.getSubject()).getDictCode());
        }
        if (StringUtils.isNotBlank(featureVo.getClassClassify())) {
            featureVo.setClassClassifyCode(dictionaryService.getDictionaryByName(featureVo.getClassClassify()).getDictCode());
        }
        if (StringUtils.isNotBlank(featureVo.getThemeName())) {
            featureVo.setThemeCode(dictionaryService.getDictionaryByName(featureVo.getThemeName()).getDictCode());
        }
        return featureVo;
    }

    public BaseResult addFeature(OptFeatureAo optFeatureAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        // 判断是否超限制
        List<OptFeatureConfig> featureConfigs = featureConfigMapper.selectList(new LambdaQueryWrapper<>());
        if (featureConfigs.size() >= 5) {
            log.error("推荐位数量超过限制【5个】");
            return CommonResult.Fail("推荐位数量超过限制【5个】");
        }
        OptFeatureConfig featureConfig = new OptFeatureConfig();
        BeanUtils.copyProperties(optFeatureAo, featureConfig);
        try {
            // 设置资源的属性
            if (featureConfigs.size() > 0) {
                Integer orderNum = featureConfigs.stream().max(Comparator.comparing(OptFeatureConfig::getOrderNum)).get().getOrderNum();
                featureConfig.setOrderNum(orderNum + 1);
            }
            ResContent resContent = contentMapper.selectById(optFeatureAo.getResId());
            featureConfig.setResName(resContent.getName());
            exchangeDictValue(featureConfig, resContent);
            featureConfigMapper.insert(featureConfig);
            return CommonResult.Ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.Fail();
        }
    }

    /**
     * 设置字典值
     *
     * @param featureConfig
     * @param resContent
     */
    private void exchangeDictValue(OptFeatureConfig featureConfig, ResContent resContent) {
        // 清空数据
        featureConfig.setThemeName(null);
        featureConfig.setPeriod(null);
        featureConfig.setLevel(null);
        featureConfig.setSubject(null);
        featureConfig.setClassClassify(null);
        // 封装数据
        String periodCode = resContent.getDicPeriodCode();
        if (StringUtils.isNotBlank(periodCode)) {
            ResDictionary dictionary = dictionaryService.getResDictionaryByCode(periodCode);
            featureConfig.setPeriod(dictionary.getDictName());
        }
        String levelCode = resContent.getDicLevelCode();
        if (StringUtils.isNotBlank(levelCode)) {
            ResDictionary dictionary = dictionaryService.getResDictionaryByCode(levelCode);
            featureConfig.setLevel(dictionary.getDictName());
        }
        String subjectCode = resContent.getDicSubjectCode();
        if (StringUtils.isNotBlank(subjectCode)) {
            ResDictionary dictionary = dictionaryService.getResDictionaryByCode(subjectCode);
            featureConfig.setSubject(dictionary.getDictName());
        }
        String themeCode = resContent.getDicThemeCode();
        if (StringUtils.isNotBlank(themeCode)) {
            ResDictionary dictionary = dictionaryService.getResDictionaryByCode(themeCode);
            featureConfig.setThemeName(dictionary.getDictName());
            if (themeCode.equals("1069")) {
                featureConfig.setClassClassify(featureConfig.getSubject());
                featureConfig.setSubject(null);
            }
        }
    }

    public BaseResult editFeature(OptFeatureAo optFeatureAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        OptFeatureConfig featureConfig = new OptFeatureConfig();
        BeanUtils.copyProperties(optFeatureAo, featureConfig);
        try {
            if (!optFeatureAo.getChangeOrder()) {
                ResContent resContent = contentMapper.selectById(optFeatureAo.getResId());
                featureConfig.setResName(resContent.getName());
                exchangeDictValue(featureConfig, resContent);
                featureConfigMapper.updateById(featureConfig);
                return CommonResult.Ok();
            } else {
                Integer originOrder = null;
                Integer targetOrder = null;
                OptFeatureConfig originObject = featureConfigMapper.selectById(optFeatureAo.getOriginId());
                OptFeatureConfig targetObject = featureConfigMapper.selectById(optFeatureAo.getTargetId());
                originOrder = originObject.getOrderNum();
                targetOrder = targetObject.getOrderNum();
                originObject.setOrderNum(targetOrder);
                targetObject.setOrderNum(originOrder);
                featureConfigMapper.updateById(originObject);
                featureConfigMapper.updateById(targetObject);
                return CommonResult.Ok();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.Fail();
        }
    }

    /**
     * 删除推荐位配置
     *
     * @param optFeatureAo
     * @return
     */
    public BaseResult deleteFeature(OptFeatureAo optFeatureAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        try {
            featureConfigMapper.deleteById(optFeatureAo.getId());
            return CommonResult.Ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.Fail();
        }
    }

}




